import axios from 'axios';
import { MessageBox, Message } from "element-ui";
// import config from '../../vue.config'
// const BASE_URL = config.devServer.proxy["/api"].target 
// console.log(BASE_URL, "配置文件");

// const base_URL = 'https://iot.tejinrui.com/api'
var request  = axios.create(
	{
		baseURL:"https://iot.tejinrui.com/api",
		timeout:5000,
		headers: {
			'Access-Control-Allow-Origin': '*'
		  }

	}
)

// request.interceptors.request.use(function(config){

// 	return config;
// })

// request.interceptors.response.use(function(res){

// 	return res.data
// },function(err){

// 	return Promise.reject(err);
// })



request.interceptors.request.use(
	(config) => {
	 
	  return config;
	},
	(error) => {
	
	  return Promise.reject(error);
	}
  );
        
request.interceptors.response.use(
	/**
	 * 处理请求错误
	 * Please return  response => response
	 */
  
	/**
	 * 通过自定义代码确定请求状态
	 * 例子
	 * 您还可以通过HTTP状态代码来判断状态。
	 */
	(response) => {
	  const res = response.data;
	  // console.log(res, "拦截的数据");
	  if (res.code === 10002) {
		Message({
		  message: res.message,
		  type: "error",
		  duration: 2 * 1000,
		});
		// store.dispatch("user/logout").then(() => {
		//   location.reload(true);
		// });
	  } else if (res.code == 10001) {
		Message({
		  message: res.message,
		  type: "error",
		  duration: 2 * 1000,
		});
	  } else {
		if (res.message) {
		  Message({
			message: res.message,
			type: "success",
			duration: 2 * 1000,
		  });
		}
		return res;
	  }
	},
	(error) => {
	  // console.log("err" + error); // for debug
	  Message({
		message: "系统/接口维护,请稍后重试",
		type: "error",
		duration: 5 * 1000,
	  });
	  return Promise.reject(error);
	}
  );



export default request;